package com.changgou.business.listen;

import com.alibaba.fastjson.util.JavaBeanInfo;
import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author: 陈安
 * @Date: 2020/12/09$ 9:28$
 * @Description: $todo$
 * @Version 1.0
 */

@Component
public class AdListener {

    //这个是监听mq的哪一个队列的,就是说一旦我们这个队列ad_update_queue有信息了,那这个监听方法就会被调起i

    @RabbitListener(queues = "ad_update_queue")
    public void receiveMessage(String message) {
        System.out.println("接收到的信息是" + message);

        //发起远程调用
        OkHttpClient okHttpClient = new OkHttpClient();
        String url = "http://192.168.200.128/ad_update?position=" + message;
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            //这个方法是当前请求是失败的
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();

            }

            @Override
            //这个方法是说当前的请求是成功的
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("请求成功" + response.message());
            }
        });


    }

}
